import {
    getCstYearProfit,
    getCurrencyProfitStatisticsForPage,
    getPayStatistics,
    getprofitStatisticsForPage,
    getReceiptStatistics,
    getRevenueStatistics,
    getStatisticsForFinance,
    getYwUserYearProfit,
    queryMarketInfo
} from '../../services/profitReport/report';
import Constants from '../../utils/Constants'

const R = require('ramda');

export default {
    namespace: 'report',
    state: {
        page: {},
        list: [],
        dataSource: {},
    },
    reducers: {
        /**
         * 刷新state
         * 
         * @param state 
         */
        refreshState(state, { payload }) {
            return { ...state, ...payload };
        }
    },
    effects: {
        /**
         * 业务利润统计表
         * 
         */
        *getprofitStatisticsForPage({ payload }, { call, put }) {
            const res = yield call(getprofitStatisticsForPage, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        dataSource: res.data.content ? res.data.content : {},
                        page: res.data.content.page ? res.data.content.page : Constants.DEFAULT_PAGE
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 获取业务员年度利润
         * 
         */
        *getYwUserYearProfit({ payload }, { call, put }) {
            const res = yield call(getYwUserYearProfit, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: res.data.content.list ? res.data.content.list : [],
                        page: res.data.content.page ? res.data.content.page : Constants.DEFAULT_PAGE
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 获取客户年度利润
         * 
         */
        *getCstYearProfit({ payload }, { call, put }) {
            const res = yield call(getCstYearProfit, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: res.data.content.list ? res.data.content.list : [],
                        page: res.data.content.page ? res.data.content.page : Constants.DEFAULT_PAGE
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 收款统计表
         * 
         */
        *getReceiptStatistics({ payload }, { call, put }) {
            const res = yield call(getReceiptStatistics, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: res.data.content.list ? res.data.content.list : [],
                        page: res.data.content.page ? res.data.content.page : Constants.DEFAULT_PAGE
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 更新收款统计表
         * 
         */
        *getRevenueStatistics({ payload }, { call, put }) {
            const res = yield call(getRevenueStatistics, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: res.data.content.list ? res.data.content.list : [],
                        page: res.data.content.page ? res.data.content.page : Constants.DEFAULT_PAGE
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 更新付款统计表
         * 
         */
        *getPayStatistics({ payload }, { call, put }) {
            const res = yield call(getPayStatistics, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: res.data.content.list ? res.data.content.list : [],
                        page: res.data.content.page ? res.data.content.page : Constants.DEFAULT_PAGE
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 利润表(财务视图)
         * 
         */
        *getStatisticsForFinance({ payload }, { call, put }) {
            const res = yield call(getStatisticsForFinance, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: res.data.content.list ? res.data.content.list : [],
                        page: res.data.content.page ? res.data.content.page : Constants.DEFAULT_PAGE
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 获取各种币种的订单利润统计
         * 
         */
        *getCurrencyProfitStatisticsForPage({ payload }, { call, put }) {
            const res = yield call(getCurrencyProfitStatisticsForPage, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: res.data.content.list ? res.data.content.list : [],
                        page: res.data.content.page ? res.data.content.page : Constants.DEFAULT_PAGE
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         * 营销线索查询
         * 
         */
        *queryMarketInfo({ payload }, { call, put }) {
            const res = yield call(queryMarketInfo, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: res.data.content.list ? res.data.content.list : [],
                        page: res.data.content.page ? res.data.content.page : Constants.DEFAULT_PAGE
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },

    },
    subscriptions: {
        setup({ history, dispatch }) {
            return history.listen(({ pathname }) => {
                const menus = JSON.parse(sessionStorage.getItem(Constants.USER_MENU));
                if(menus && !menus.find(item=>item.includes(pathname)) && pathname != '/logout'){
                    document.location.href = "/notAuthorized"
                }
            });
        }
    }
};